package se.jocke.nb.proxy.ui;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import org.openide.explorer.view.Visualizer;
import org.openide.nodes.Node;
import se.jocke.nb.proxy.ui.node.HttpMessageNode;

/**
 *
 * @author jocke
 */
public class HttpMessageListView extends org.openide.explorer.view.ListView {

    private JList jList;

    @Override
    protected JList createList() {
        jList = super.createList();
        jList.setOpaque(true);
        jList.setBackground(getBackground());
        jList.setForeground(getForeground());
        jList.setCellRenderer(new ListCellRendererImpl());
        return jList;
    }

    @Override
    public void setBackground(Color color) {
        super.setBackground(color);
        if (jList != null) {
            jList.setBackground(color);
        }
    }

    @Override
    public void setForeground(Color color) {
        super.setForeground(color);
        if (jList != null) {
            jList.setForeground(color);
        }
    }

    @Override
    public void setOpaque(boolean opaque) {
        super.setOpaque(opaque);
        if (jList != null) {
            jList.setOpaque(opaque);
        }
    }

    private static class ListCellRendererImpl implements ListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int row, boolean isSelected, boolean hasFocus) {
            Node node = Visualizer.findNode(value);
            HttpMessageCell cell = new HttpMessageCell((HttpMessageNode) node);

            if (isSelected) {
                cell.setBackground(list.getSelectionBackground());
                cell.setForeground(list.getSelectionForeground());

            } else {
                cell.setBackground(list.getBackground());
                cell.setForeground(list.getForeground());
            }

            return cell;
        }

    }
}
